package com.example.big_orange2.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.big_orange2.authorization.annotation.AuthIgnore;
import com.example.big_orange2.common.Result;
import com.example.big_orange2.entity.Staff;
import com.example.big_orange2.entity.lockorder;
import com.example.big_orange2.entity.orderdata;
import com.example.big_orange2.entity.sharelock;
import com.example.big_orange2.mapper.StaffMapper;
import com.example.big_orange2.mapper.lockorderMapper;
import com.example.big_orange2.mapper.sharelockMapper;
import com.example.big_orange2.service.LockOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/lockorder")
public class lockorderController {

    @Resource
    lockorderMapper lockorderMapper;
    @Resource
    sharelockMapper sharelockMapper;
    @Resource
    LockOrderService lockOrderService;
    //增
    //向数据库写入（insert）数据的接口
    @PostMapping("/add")
    public Result<?> save(@RequestBody lockorder lockorder){//是泛型，表示任意类型
        //设置默认值
        //插入的操作
        lockorderMapper.insert(lockorder);

        sharelock lock =new sharelock();
        lock.setSharelockid(lockorder.getSharelockid());
        lock.setSharelockstatus("Isuse");
        sharelockMapper.updateById(lock);

        return Result.success(lockorder.getLockorderid());//返回操作情况
    }
    @AuthIgnore
    @PutMapping("/endorder/{alltime}")
    public Result<?> endorder(@PathVariable Float alltime,@RequestBody lockorder lockorder){//?是泛型，表示任意类型
          lockOrderService.endorder(lockorder.getLockorderid(),lockorder.getSharelockid(),alltime,lockorder.getUserid());
//        lockorderMapper.endorder(lockorder.getLockorderid(),lockorder.getSharelockid(),alltime,lockorder.getUserid());
        return Result.success();//返回操作情况
    }
    //向数据库删除（delete）数据的接口
    @DeleteMapping("/delete/{lockorderid}")//占位
    public Result<?> delete(@PathVariable String lockorderid){//?是泛型，表示任意类型
        //删除的操作
        lockorderMapper.deleteById(lockorderid);
        return Result.success();//返回操作情况

    }
    //修改功能
    @PutMapping("/update")
    //向数据库更新（update）数据的接口
    public Result<?> update(@RequestBody lockorder lockorder){//?是泛型，表示任意类型
        lockorderMapper.updateById(lockorder);
        return Result.success();
    }

    //全部查询
//    @AuthIgnore
    @GetMapping("/getdata")
    //根据员工号，挑选他负责的客户信息
    public Result<?> finduser(){
        LambdaQueryWrapper<lockorder> wrapper = Wrappers.<lockorder>lambdaQuery();
        List<lockorder> Loanhandle = lockorderMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }

    //依据序号id查询
    @GetMapping("/getdataseq/{lockorderid}")
    //根据员工号，挑选他负责的客户信息
    public Result<?> findbyseq(@PathVariable String lockorderid){
        LambdaQueryWrapper<lockorder> wrapper =Wrappers.<lockorder>lambdaQuery();
        wrapper.eq(lockorder::getLockorderid,lockorderid);//多条件查询
        wrapper.orderByDesc(lockorder::getStarttime);
        List<lockorder> Loanhandle = lockorderMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }
    //依据用户号查询
    @GetMapping("/getdatauser/{userid}")
    public Result<?> findbyuser(@PathVariable String userid){
        LambdaQueryWrapper<lockorder> wrapper =Wrappers.<lockorder>lambdaQuery();
        wrapper.eq(lockorder::getUserid,userid);//多条件查询
        wrapper.orderByDesc(lockorder::getStarttime);
        List<lockorder> Loanhandle = lockorderMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }
    //依据用户号查询当前正在使用设备
    @GetMapping("/getdatanow/{userid}")
    public Result<?> findbynow(@PathVariable String userid){
        LambdaQueryWrapper<lockorder> wrapper =Wrappers.<lockorder>lambdaQuery();
        wrapper.eq(lockorder::getUserid,userid);//多条件查询
        wrapper.eq(lockorder::getComplete,"0");
        wrapper.orderByDesc(lockorder::getStarttime);
        List<lockorder> Loanhandle = lockorderMapper.selectList(wrapper);//
        return  Result.success(Loanhandle);//返回结果

    }
    //依据用户号查询当前正在使用设备
    @GetMapping("/getmogneydata")
    public Result<?> getmoneydata(){
        List<lockorder> Loanhandle = lockorderMapper.getmongeydata();//
        return  Result.success(Loanhandle);//返回结果
    }
    //全部查询
    @AuthIgnore
    @GetMapping("/getorderdata")
    //根据员工号，挑选他负责的客户信息
    public Result<?> getorderdata(){
        List<orderdata> Loanhandle = lockorderMapper.getorderdata();//
        return  Result.success(Loanhandle);//返回结果

    }
}
